package hash;

import java.util.HashMap;
import java.util.Map;

/**
 * 383 ransom 赎金信
 */
public class Ransom {

    public static void main(String[] args) {
        String str1 = "ransom";
        String str2 = "magazine";
        System.out.println(canConstruct(str1,str2));
    }

    private static boolean canConstruct(String str1,String str2) {
        Map<Character,Integer> map = new HashMap<>(str2.length());
        for (char c: str2.toCharArray()) {
            map.put(c, map.getOrDefault(c,0) + 1);
        }

        for (char c: str1.toCharArray()) {
            Integer count = map.get(c);
            if (count == null || count.equals(0)) {
                return false;
            } else {
                map.put(c,map.get(c) -1);
            }
        }
        return true;
    }
}
